<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTools :tree-node="company" :is-root="true" @dialogShow="addDept" />
        <el-tree
          :empty-text="''"
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepartment="getDepartments"
            @dialogShow="addDept"
          />
        </el-tree>
      </el-card>
    </div>
    <AddDept
      v-if="dialogVisible"
      :dialog-visible.sync="dialogVisible"
      :node="node"
      :is-edit="isEdit"
      @addDepartment="getDepartments"
    />
  </div>
</template>

<script>
import TreeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name'
      },
      company: {},
      dialogVisible: false, // 控制弹层的显示
      node: null, // 点击的部门信息
      isEdit: null // 是否是编辑状态
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(res.depts, '') // 需要将其转化成树形结构
      // console.log(res)
    },
    addDept(node, isEdit = false) {
      this.dialogVisible = true
      this.node = node
      this.isEdit = isEdit
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
